package cn.tedu.fengyuback.personal.service.impl;

import cn.tedu.fengyuback.personal.mapper.PersonalMapper;
import cn.tedu.fengyuback.personal.pojo.dto.*;
import cn.tedu.fengyuback.personal.pojo.entity.Articles;
import cn.tedu.fengyuback.personal.pojo.entity.Favorites;
import cn.tedu.fengyuback.personal.pojo.entity.Likes;
import cn.tedu.fengyuback.personal.pojo.entity.UserFollows;
import cn.tedu.fengyuback.personal.pojo.vo.*;
import cn.tedu.fengyuback.personal.service.PersonalService;
import cn.tedu.fengyuback.user.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Slf4j
@Service
public class PersonalServiceImpl implements PersonalService {
    @Autowired
    PersonalMapper personalMapper;
    @Autowired
    UserMapper userMapper;

    @Override
    public List<UserFollowsVO> selectUserFollows(Long id) {
        List<UserFollowsVO> list = personalMapper.selectUserFollows(id);
        //获取关注用户的昵称
        for (UserFollowsVO userFollowsVO : list){
            PersonalQuery personalQuery = new PersonalQuery();
            personalQuery.setId(userFollowsVO.getFollowsUserId());
            UserVO userVO = personalMapper.select(personalQuery);
            userFollowsVO.setNickname(userVO.getNickname());

        }
        return list;
    }
    @Override
    public List<UserVO> selectUserUnFollows(Long id) {
        log.debug("查询未关注用户的参数："+id);
        List<UserVO> list = personalMapper.selectUserUnFollows(id);
        log.debug("未关注用户的集合："+list);
        //获取未关注用户的昵称
        for (UserVO userVO : list){
            PersonalQuery personalQuery = new PersonalQuery();
            personalQuery.setId(userVO.getId());
            log.debug("查询未关注用户的参数："+personalQuery);
            UserVO userVO1 = personalMapper.select(personalQuery);
            log.debug("未关注用户的昵称："+userVO1.getNickname());
            userVO.setNickname(userVO1.getNickname());
        }
        log.debug("未关注用户的集合："+list);
        return list;


    }

    @Override
    public void addFollows(Long id,Long data) {
        log.debug("关注用户的id："+id);
        log.debug("用户的id："+data);
        UserFollows userFollows = new UserFollows();
        userFollows.setUserId(data);
        userFollows.setFollowsUserId(id);
        personalMapper.insert(userFollows);
        log.debug("关注用户的参数："+userFollows);
    }
    //发表文章
    @Override
    public void releaseArticle(ArticlesPram articlesPram) {
        log.debug("发布文章的参数："+articlesPram);
        Articles articles = new Articles();
        BeanUtils.copyProperties(articlesPram,articles);
        log.debug("复制完的文章的参数："+articles);
        articles.setQuestionId(1L);
        articles.setLikes("0");
        articles.setFavorites("0");
        articles.setViews("100");
        articles.setStatus("1");
        articles.setCreateTime(new Date());
        personalMapper.insertArticles(articles);
        log.debug("发布文章的参数："+articles);
    }


    //新增关注


    @Override
    public void deleteByFollowsUserId(Long followsUserId) {
        personalMapper.deleteByFollowsUserId(followsUserId);
    }

//查询文章
    @Override
    public List<ArticlesVO> selectByIdArticle(Long uId,Long userId,Long questionId,String status,Long category,Long subcategory,Long locCategory) {
        log.debug("查询用户发布文章的参数："+ uId+" "+ userId+" "+questionId+" "+status+" "+category+" "+subcategory +" "+locCategory);

        ArticlesQuery articlesQuery = new ArticlesQuery();
        articlesQuery.setUId(uId);
        articlesQuery.setQuestionId(questionId);
        articlesQuery.setStatus("0");
        log.debug("查询参数："+articlesQuery);
        List<ArticlesVO> list;
        if(status.equals("1")){
            articlesQuery.setCategory(category);
            articlesQuery.setSubcategory(subcategory);
            if(locCategory!=null){
                articlesQuery.setLocCategory(locCategory);
            }
            articlesQuery.setUserId(userId);
             list = personalMapper.selectFollowArticle(articlesQuery);
            log.debug("查询用户关注文章："+list);
        }
        else if(status.equals("2")){
            articlesQuery.setUserId(userId);
            list = personalMapper.selectByIdArticle(articlesQuery);
        }
        else if(status.equals("5")||status.equals("3")||status.equals("4")){
            log.debug( "查询是否关注："+uId);
            articlesQuery.setCategory(category);
            articlesQuery.setSubcategory(subcategory);
            articlesQuery.setLocCategory(locCategory);
            if(uId==userId){
                log.debug("查询已关注文章");
                articlesQuery.setUserId(userId);
                list = personalMapper.selectFollowArticle(articlesQuery);
            }
            else {
                list = personalMapper.selectByIdArticle(articlesQuery);
            }
        }
        else if(status.equals("6")||status.equals("7")||status.equals("8")){
            articlesQuery.setCategory(category);
            articlesQuery.setSubcategory(subcategory);
            articlesQuery.setLocCategory(locCategory);
            if(uId==userId){
                log.debug("查询已关注文章");
                articlesQuery.setUserId(userId);
                list = personalMapper.selectFollowArticle(articlesQuery);
            }
            else {
                list = personalMapper.selectByIdArticle(articlesQuery);
            }
        }
        else {
            list = personalMapper.selectByIdArticle(articlesQuery);
        }
        for (ArticlesVO articlesVO : list){
            //查询是否点赞
            likesQuery(articlesVO, userId);
            //查询是否收藏
           favoritesQuery(articlesVO, userId);
        }
        log.debug("查询用户发布文章："+list);
        return list;
    }

    @Override
    public List<ArticlesVO> selectByIdArticle1(ArticlesQuery articlesQuery) {
        log.debug("查询用户发布文章的参数：{}",articlesQuery);
        List<ArticlesVO> list;
        list = personalMapper.selectByIdArticle(articlesQuery);
        //查询用户发布的文章，用户自己是否点赞、收藏
        for (ArticlesVO articlesVO : list){
            //查询是否点赞
            likesQuery(articlesVO, articlesQuery.getUserId());
            //查询是否收藏
            favoritesQuery(articlesVO, articlesQuery.getUserId());
        }
//        System.out.println("================"+list);
        log.debug("查询用户发布文章："+list);
        return list;
    }

    /**
     * 查询用户是否收藏该文章
     * @param articlesVO
     * @param userId
     */
    private void favoritesQuery(ArticlesVO articlesVO, Long userId) {
        FavoritesQuery favoritesQuery = new FavoritesQuery();
        favoritesQuery.setArticleId(articlesVO.getId());
        favoritesQuery.setUserId(userId);
        FavoritesVO favoritesVO = personalMapper.selectFavorites(favoritesQuery);
        log.debug("查询收藏参数："+favoritesQuery);
        log.debug("查询收藏："+favoritesVO);
        if (favoritesVO != null){
            articlesVO.setFavoritesId(favoritesVO.getId());
            articlesVO.setFavoritesStatus("primary");
        }else {
            articlesVO.setFavoritesStatus("");
        }
    }

    /**
     * 查询用户是否点赞该文章
     * @param articlesVO
     */
    private void likesQuery(ArticlesVO articlesVO, Long userId) {
        log.debug("查询后端点赞参数："+articlesVO);
        log.debug("查询用户id："+userId);
        LikesQuery likesQuery = new LikesQuery();
        likesQuery.setArticleId(articlesVO.getId());
        likesQuery.setUserId(userId);
        log.debug("查询点赞参数："+likesQuery);
        LikesVO likesVO = personalMapper.selectLikes(likesQuery);
        log.debug("查询点赞："+likesVO);
        if (likesVO != null){
            articlesVO.setLikesId(likesVO.getId());
            articlesVO.setLikesStatus("primary");
        }else {
            articlesVO.setLikesStatus("");
        }
    }
    //修改点赞状态
    @Override
    public void changeLikesStatus(LikesQuery likesQuery) {
        log.debug("前端点赞数参数："+likesQuery);
        LikesVO likesVO = personalMapper.selectLikes(likesQuery);
        log.debug("点赞状态："+likesVO);
        Likes likes = new Likes();
        BeanUtils.copyProperties(likesQuery,likes);
        log.debug("点赞数参数："+likes);
        //判断文章的点赞状态
        if (likesVO == null){//文章未点赞，进行点赞操作
            likes.setLikeTime(new Date());
            personalMapper.insertLikes(likes);
            addLike(likesQuery);
        }else {//文章已点赞，取消点赞操作
            personalMapper.deleteLikes(likes);
            log.debug("删除点赞数据："+likes);
            minusLike(likesQuery);
        }
    }

    /**
     * 减少文章点赞数
     * @param likesQuery
     */
    private void minusLike(LikesQuery likesQuery) {
        Articles articles = new Articles();
        Long num = likesQuery.getLikes() - 1;
        articles.setLikes(num.toString());
        articles.setId(likesQuery.getArticleId());
        log.debug("减少文章点赞数参数："+articles);
        personalMapper.updateArticles(articles);
        log.debug("减少文章点赞数参数1："+articles);
    }

    /**
     * 增加文章表点赞数
     * @param likesQuery
     */
    private void addLike(LikesQuery likesQuery) {
        log.debug("前端点赞数参数："+likesQuery);
        Articles articles = new Articles();
        Long num = likesQuery.getLikes() + 1;
        articles.setLikes(num.toString());
        articles.setId(likesQuery.getArticleId());
        log.debug("增加文章点赞数参数："+articles);
        personalMapper.updateArticles(articles);
        log.debug("增加文章点赞数参数1："+articles);
    }

    @Override
    public void changeFavoritesStatus(FavoritesQuery favoritesQuery) {
        FavoritesVO favoritesVO = personalMapper.selectFavorites(favoritesQuery);
        Favorites favorites = new Favorites();
        BeanUtils.copyProperties(favoritesQuery,favorites);
        //判断文章的收藏状态
        if (favoritesVO == null){//为收藏
            favorites.setFavoritesTime(new Date());
            personalMapper.insertFavorites(favorites);
            addArticles(favoritesQuery);
        }else {//已收藏
            personalMapper.deleteFavorites(favorites);
            minusArticles(favoritesQuery);
        }
    }



    /**
     * 减少文章收藏数
     * @param favoritesQuery
     */
    private void minusArticles(FavoritesQuery favoritesQuery) {
        Articles articles = new Articles();
        Long num = favoritesQuery.getFavorites() - 1;
        articles.setFavorites(num.toString());
        articles.setId(favoritesQuery.getArticleId());
        log.debug("减少文章收藏数参数："+articles);
        personalMapper.updateArticles(articles);
    }

    /**
     * 增加文章表收藏数
     * @param favoritesQuery
     */
    private void addArticles(FavoritesQuery favoritesQuery) {
        Articles articles = new Articles();
        Long num = favoritesQuery.getFavorites() + 1;
        articles.setFavorites(num.toString());
        articles.setId(favoritesQuery.getArticleId());
        log.debug("增加文章收藏数参数："+articles);
        personalMapper.updateArticles(articles);
    }
}
